'/
' mathutil - version 1.033
' Written 2010 by Anton Berlin.
' 
' =========================================================================
' 
' This program is free software; you can redistribute it and/or modify
' it under the terms of the GNU Lesser General Public License as published
' by the Free Software Foundation; either version 3 of the License.
' 
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
' GNU General Public License for more details.
' 
' You should have received a copy of the GNU Lesser General Public License
' along with this program; if not, write to the Free Software
' Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
'/

superstrict

import "Vector2.bmx"

type Rectangle

	'// -----------------------------------------------------------------------------------------------------------------
	'// Description:
	'// 		Create, setup and return a new rectangle.
	'// -----------------------------------------------------------------------------------------------------------------
	function create:Rectangle(_x:int, _y:int, _width:int, _height:int)
	
		local rect:Rectangle = new Rectangle;
		
		rect.X = _x;
		rect.Y = _y;
		rect.width = _width;
		rect.height = _height;
		
		return rect;
	
	end function
	
	'// -----------------------------------------------------------------------------------------------------------------
	'// Description:
	'// 		Check to see if this rectangle intersects with the rectangle you pass to this function.
	'// -----------------------------------------------------------------------------------------------------------------
	method intersects:byte(rect:Rectangle)
	
		return ..
			left() < rect.right() and ..
			right() > rect.left() and ..
			top() < rect.bottom() and ..
			bottom() > rect.top();
	
	end method
	
	'// -----------------------------------------------------------------------------------------------------------------
	'// Description:
	'// 		Return how much this rectangle overlaps the specified rectangle.
	'// -----------------------------------------------------------------------------------------------------------------
	method getOverlap:Vector2(rect:Rectangle)
	
		local overlap:Vector2 = new Vector2;
		
		local thisCenter:Vector2 = centerRelativeToScreen();
		local rectCenter:Vector2 = rect.centerRelativeToScreen();
		
		if (thisCenter.X < rectCenter.X) ..
			overlap.X = min(rectCenter.X, right() - rect.left());
		if (thisCenter.X >= rectCenter.X) ..
			overlap.X = min(rectCenter.X, left() - rect.right());
		if (thisCenter.Y < rectCenter.Y) ..
			overlap.Y = min(rectCenter.Y, bottom() - rect.top());
		if (thisCenter.Y >= rectCenter.Y) ..
			overlap.Y = min(rectCenter.Y, top() - rect.bottom());
		
		return overlap;
	
	end method
	
	'// -----------------------------------------------------------------------------------------------------------------
	'// Description:
	'// 		Returns the center point of this rectangle, relative to the screen.
	'// -----------------------------------------------------------------------------------------------------------------
	method centerRelativeToScreen:Vector2()
	
		return Vector2.create(X + width / 2.0, Y + height / 2.0);
	
	end method
	
	'// -----------------------------------------------------------------------------------------------------------------
	'// Description:
	'// 		Returns the center point of this rectangle, relative to the rectangle.
	'// -----------------------------------------------------------------------------------------------------------------
	method centerRelativeToRect:Vector2()
	
		return Vector2.create(width / 2.0, height / 2.0);
	
	end method
	
	'// -----------------------------------------------------------------------------------------------------------------
	'// Description:
	'// 		Return the X-coordinate of the left side of the rectangle.
	'// -----------------------------------------------------------------------------------------------------------------
	method left:float()
	
		return X;
	
	end method
	
	'// -----------------------------------------------------------------------------------------------------------------
	'// Description:
	'// 		Return the X-coordinate of the right side of the rectangle.
	'// -----------------------------------------------------------------------------------------------------------------
	method right:float()
	
		return X + width;
	
	end method
	
	'// -----------------------------------------------------------------------------------------------------------------
	'// Description:
	'// 		Return the Y-coordinate of the top of the rectangle.
	'// -----------------------------------------------------------------------------------------------------------------
	method top:float()
	
		return Y;
	
	end method
	
	'// -----------------------------------------------------------------------------------------------------------------
	'// Description:
	'// 		Return the Y-coordinate of the bottom of the rectangle.
	'// -----------------------------------------------------------------------------------------------------------------
	method bottom:float()
	
		return Y + height;
	
	end method
	
	'// -----------------------------------------------------------------------------------------------------------------
	'// Description:
	'// 		Return a string representation of the rectangle.
	'// -----------------------------------------------------------------------------------------------------------------
	method toString:string()
	
		return "Top left: " + left() + "x" + top() + ", bottom right: " + right() + "x" + bottom();
	
	end method
	
	'// -----------------------------------------------------------------------------------------------------------------
	'// Description:
	'// 		The position of the rectangle.
	'// -----------------------------------------------------------------------------------------------------------------
	field X:float;
	field Y:float;
	
	'// -----------------------------------------------------------------------------------------------------------------
	'// Description:
	'// 		The size of the rectangle.
	'// -----------------------------------------------------------------------------------------------------------------
	field width:float;
	field height:float;

end type
